﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Event_Planner
{
    public partial class Entrence_Application : Form
    {
        List<Participant> participantList;
        DB db;

        public Entrence_Application()
        {
            InitializeComponent();
            db = new DB();
            participantList = new List<Participant>();
            showInfo();
        }
        public void showInfo()
        {
            this.dataGridView1.Rows.Clear();
            participantList = getAllParticipants();
            foreach (Participant p in participantList)
            {
                this.dataGridView1.Rows.Add(p.Number, p.FirstName, p.LastName, p.Email, p.Address, 
                    p.AccountNumber, p.Balance, p.RegistrationDate, p.DateOfBirth);
            }
        }

        public List<Participant> getAllParticipants()
        {
            List<Participant> participants = new List<Participant>();
            DataTable participantRecords = db.getAllParticipantsFromDB();
            for (int i = 0; i < participantRecords.Rows.Count; i++)
            {
                Participant participant = new Participant();
                participant.Number = Convert.ToInt32(participantRecords.Rows[i]["participant_id"]);
                participant.FirstName = participantRecords.Rows[i]["first_name"].ToString();
                participant.LastName = participantRecords.Rows[i]["last_name"].ToString();
                participant.Email = participantRecords.Rows[i]["email"].ToString();
                participant.Address = participantRecords.Rows[i]["address"].ToString();
                participant.DateOfBirth = participantRecords.Rows[i]["date_of_birth"].ToString();
                participant.RegistrationDate = participantRecords.Rows[i]["registration_date"].ToString();
                participant.PaymentStatus = Convert.ToInt32(participantRecords.Rows[i]["payment_status"]);
                participant.AttendanceStatus = Convert.ToInt32(participantRecords.Rows[i]["attendance_status"]);
                participant.AccountNumber = Convert.ToInt32(participantRecords.Rows[i]["account_number"]);
                participant.Balance = Convert.ToInt32(participantRecords.Rows[i]["balance"]);
                participants.Add(participant);
            }
            return participants;
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            if (this.txtType.Text == "")
            {
                MessageBox.Show("You must specify the search criterion");
            }
            else
            {
                if (rbSearchId.Checked == true)
                {
                    int number = Convert.ToInt32(this.txtType.Text);
                    clearLabels();
                    participantList = getAllParticipants();
                    foreach (Participant p in participantList)
                    {
                        if (p.Number == number)
                        {
                            this.labelNumber.Text = p.Number.ToString();
                            this.labelFirstName.Text = p.FirstName;
                            this.labelLastName.Text = p.LastName;
                            this.labelEmail.Text = p.Email;
                            this.labelDate.Text = p.DateOfBirth;
                            this.labelAddress.Text = p.Address;
                            if (p.PaymentStatus == 1)
                            {
                                this.txtStatus.BackColor = Color.Green;
                            }
                            else
                            {
                                this.txtStatus.BackColor = Color.Red;
                            }
                            break;
                        }
                    }
                }
                else if (rbSearchEmail.Checked == true)
                {
                    string email = this.txtType.Text;
                    clearLabels();
                    foreach (Participant p in participantList)
                    {
                        if (p.Email == email)
                        {
                            this.labelNumber.Text = p.Number.ToString();
                            this.labelFirstName.Text = p.FirstName;
                            this.labelLastName.Text = p.LastName;
                            this.labelEmail.Text = p.Email;
                            this.labelDate.Text = p.DateOfBirth;
                            this.labelAddress.Text = p.Address;
                            if (p.PaymentStatus==1)
                            {
                                this.txtStatus.BackColor = Color.Green;
                            }
                            else
                            {
                                this.txtStatus.BackColor = Color.Red;
                            }
                            break;
                        }
                    }
                }
            }
        }
        public void clearLabels()
        {
            this.labelNumber.Text = "";
            this.labelFirstName.Text = "";
            this.labelLastName.Text = "";
            this.labelEmail.Text = "";
            this.labelDate.Text = "";
            this.labelAddress.Text = "";
            this.txtStatus.BackColor = Color.Red;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            if (this.txtUserId2.Text == "")
            {
                MessageBox.Show("Enter the User's Id");
            }
            else
            {
                int id = Convert.ToInt32(this.txtUserId2.Text);
                if (id > 0)
                {
                    this.dataGridView1.Rows.Clear();
                    foreach (Participant p in participantList)
                    {
                        if (p.Number == id)
                        {
                            this.dataGridView1.Rows.Add(p.Number, p.FirstName, p.LastName, p.Email, p.Address, p.AccountNumber, p.Balance, p.RegistrationDate, p.DateOfBirth);
                        }
                    }
                }
                else
                {
                    MessageBox.Show("Enter a valid Id");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (this.txtParticipantId.Text == "" || this.txtAmount.Text == "")
            {
                MessageBox.Show("Enter both the participant Id and the amount");
            }
            else
            {
                int id = Convert.ToInt32(this.txtParticipantId.Text);
                int amount = Convert.ToInt32(this.txtAmount.Text);
                if (id > 0 && amount > 0)
                {
                    int accountNumber = db.getUserAccountNumber(id);
                    Participant participant = getParticipant(id);
                    db.depositParticipantAccount(participant.AccountNumber, amount);
                    db.insertAccountTransfer(participant.Number, amount);
                    MessageBox.Show("The deposit was successful");
                    showInfo();
                }
                else
                {
                    MessageBox.Show("The participant Id or the amount is not valid");
                }
            }
        }
        public Participant getParticipant(int id)
        {
            foreach (Participant p in participantList)
            {
                if (p.Number == id)
                {
                    return p;
                }
            }
            return null;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPayment_Click(object sender, EventArgs e)
        {
            if (this.txtParticipantId.Text == "" || this.txtAmount.Text == "")
            {
                MessageBox.Show("Enter both the participant Id and the amount");
            }
            else
            {
                //payment amount for the event
                int paymentAmount = 35;
                int id = Convert.ToInt32(this.txtParticipantId.Text);
                int amount = Convert.ToInt32(this.txtAmount.Text);
                if (id > 0 && amount > 0)
                {
                    if (amount < paymentAmount)
                    {
                        MessageBox.Show("The Payment amount is :" + paymentAmount);
                    }
                    else
                    {
                        Participant participant = getParticipant(id);
                        //insert payment into database
                        db.updateParticipantPaymentStatus(participant.Number);
                        db.updateParticipantAttendanceStatus(participant.Number,1);

                        if (amount > paymentAmount)
                        {
                            int accountNumber = db.getUserAccountNumber(id);
                            db.depositParticipantAccount(participant.AccountNumber, amount-paymentAmount);
                            db.insertAccountTransfer(participant.Number, amount-paymentAmount);

                            MessageBox.Show("The Payment was successful and the difference of "+(amount-paymentAmount)+" was transfered to your bank account");
                            showInfo();
                        }
                        else 
                        {                            
                            MessageBox.Show("The Payment was successful");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("The participant Id or the amount is not valid");
                }
            }
        }

        private void rbSearchId_CheckedChanged(object sender, EventArgs e)
        {
            if (rbSearchId.Checked)
            {
                this.labelType.Text = "ID";
            }
            else
            {
                this.labelType.Text = "Email";
            }
        }
    }
}
